/*
 * buf_CppReadBuffer_t.c
 *
 *  Created on: 2012/04/01
 *      Author: guanxiaoying
 */
#include "../../parser/coredef.h"
#define BufType  PSZ
#define Buffer stCppReadBufManager.memoryBlocks
#define Buflen stCppReadBufManager.maxRecordNum
#define  bufLenPerBlk  (stCppReadBufManager.astMaxNum_Block)

/* get the object */
static TABLEMANAGER stCppReadBufManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		1,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};
static VOID resetBufListener( U4  procid, U2 index )
{
	if( procid == stCppReadBufManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			Buffer[ index-1] = NULL;
		}
	}
}
static BufType core_getBuf( AST index )
{
	BufType temp = Check_Com( &stCppReadBufManager, resetBufListener, index );
	if( temp != NULL ){
		return &temp[index%bufLenPerBlk];
	}
	return NULL;
}
static BOOL AllocBuffer( AST astReqArg )
{
	return AllocBuffer_Com( &stCppReadBufManager, astReqArg, resetBufListener );
}
//		BOOL (*clear)( VOID );
static BOOL ClearBuffer( VOID )
{
	return Clear_Com( &stCppReadBufManager );
}
static BOOL ResetBuffer( VOID )
{
	return Reset_Com( &stCppReadBufManager );
}
static BOOL EmptyBuffer( VOID )
{
	return AllocBuffer( - Buflen );
}
/*  get method */
static CHAR * getString( AST index )
{
	BufType pTbl = core_getBuf( index );
	if( pTbl == NULL )
	{
		return "";
	}
	return pTbl;
}
/* set method */
static AST  setString( PSZ str )
{
	S4 lenBuf = str_length(str);
	AST astHeaderNo;
	if( lenBuf > 0 && lenBuf < MemGetBlockSize( ))
	{
		if( AllocBuffer( lenBuf + 1 ) ) {
			lenBuf = Buflen - lenBuf;
			astHeaderNo = lenBuf/stCppReadBufManager.astMaxNum_Block;
			strcpy( (PSZ)Buffer[astHeaderNo]+lenBuf%stCppReadBufManager.astMaxNum_Block, str );
			*((PSZ)Buffer[astHeaderNo]+Buflen%stCppReadBufManager.astMaxNum_Block) = '\0';
		}
		else
		{
			return 0;
		}
	}
	else
	{
		if ( 0 == AllocBuffer( lenBuf + 1 ) )
		{
			return 0;
		}
		lenBuf = Buflen;
	}
	return lenBuf;
}
/* set method */
static AST  appendString( AST index, PSZ str )
{
	S4 lenBuf_New;
	S4 lenBuf_Old;
	AST astHeaderNo;
	PSZ pTbl;

	if( Buflen == 0 )
	{
		return setString(str);
	}
	pTbl = core_getBuf( index );
	lenBuf_Old = str_length(pTbl);
	if( lenBuf_Old == 0 )
	{
		return setString(str);
	}
	lenBuf_New = str_length(str);
	if( lenBuf_New == 0 )
	{
		return index;
	}
	if( index + lenBuf_Old >= Buflen )
	{
		if( Buflen%stCppReadBufManager.astMaxNum_Block + lenBuf_New < stCppReadBufManager.astMaxNum_Block )
		{
			if( AllocBuffer( lenBuf_New ) ) {
				lenBuf_Old = index + lenBuf_Old;
				astHeaderNo = lenBuf_Old/stCppReadBufManager.astMaxNum_Block;
				strcpy( (PSZ)Buffer[astHeaderNo]+lenBuf_Old%stCppReadBufManager.astMaxNum_Block, str );
				*((PSZ)Buffer[astHeaderNo]+Buflen%stCppReadBufManager.astMaxNum_Block) = '\0';
			}
			else
			{
				index = 0;
			}
			return index;
		}
	}
	lenBuf_New += lenBuf_Old;
	if( lenBuf_New > 0 && lenBuf_New < MemGetBlockSize( ))
	{
		if( AllocBuffer( lenBuf_New + 1 ) ) {
			lenBuf_New = Buflen - lenBuf_New;
			astHeaderNo = lenBuf_New/stCppReadBufManager.astMaxNum_Block;
			strcpy( (PSZ)Buffer[astHeaderNo]+lenBuf_New%stCppReadBufManager.astMaxNum_Block,
					(PSZ)Buffer[index/stCppReadBufManager.astMaxNum_Block]+index%stCppReadBufManager.astMaxNum_Block );
			index = lenBuf_New;
			lenBuf_New += lenBuf_Old;
			astHeaderNo = lenBuf_New/stCppReadBufManager.astMaxNum_Block;
			strcpy( (PSZ)Buffer[astHeaderNo]+lenBuf_New%stCppReadBufManager.astMaxNum_Block, str );
			*((PSZ)Buffer[astHeaderNo]+Buflen%stCppReadBufManager.astMaxNum_Block) = '\0';
		}
		else
		{
			index = 0;
		}
	}
	else
	{
		if ( AllocBuffer( 1 ) )
		{
			index = Buflen;
		}
		else
		{
			index = 0;
		}

	}
	return index;
}
static BOOL  deleteString( AST cnt )
{
	return AllocBuffer( -cnt );
}
BOOL isBlank( VOID  ){
	return ( Buflen == 0 );
}
const ACCESS_READBUFFER stCppReadBufferAccess = {
		EmptyBuffer,
//		BOOL (*clear)( VOID );
		ClearBuffer,
//		BOOL (*reset)( VOID );
		ResetBuffer,
//		BOOL (*deleteChar)( AST  );
		deleteString,
//		CHAR * (*getStr)( AST index );
		getString,
//		AST (*addStr)( PSZ str );
		setString,
//		AST (*appendStr)( AST index, PSZ str );
		appendString,
		isBlank
};
